﻿<!DOCTYPE html>
<html>
<head>
    <title>链接添加修改</title>
    $css()
    
</head>
<body>
    <style type="text/css">
        #bind {
            width: 300px;
            position: absolute;
            left: 350px;
            top: 20px;
            line-height: 30px;
        }

        .archivelist {
            line-height: 20px;
        }
    </style>
    <form action="" method="post">


        <div class="area" id="dataPanel">
            <div class="form">
                <input type="hidden" field="Type" />
                <input type="hidden" field="Id" />

                <dl>
                    <dt>绑定:</dt>
                    <dd>
                        <input type="hidden" field="BindId" id="bindid" />
                        <input type="hidden" field="BindType" id="bindtype" />

                        <span id="bindtip" field="BindTitle"></span>&nbsp;&nbsp;
                        <a href="javascript:cancelBind()" id="cancelbind">取消绑定</a>

                    </dd>
                </dl>
                <dl>
                    <dt>文&nbsp;&nbsp;&nbsp;&nbsp;字：</dt>
                    <dd>
                        <input type="text" class="ui-validate" required="true" length="[0,50]" field="Text" id="text" />
                    </dd>
                </dl>
                <dl id="urlpanel">
                    <dt>地&nbsp;&nbsp;&nbsp;&nbsp;址：</dt>
                    <dd>
                        <input type="text" class="ui-validate" length="[0,255]" field="Uri" id="uri" size="45" />
                        <br /><span class="msg" style="color:#777;font-size:smaller;padding-left:65px;">路径请以"/"开头,外站链接请加"http://"</span>
                    </dd>
                </dl>
                <dl>
                    <dt>图片：</dt>
                    <dd>
                        <input type="text" class="ui-validate" length="[0,100]" field="ImgUrl" id="imgurl" tipin="imgurl_tip" />
                        <span id="imgurl_tip">选填</span><br />
                        <span style="display:block;padding-left:55px;" id="upload">上传文件</span>

                    </dd>
                </dl>
                <dl>
                    <dt>顺&nbsp;&nbsp;&nbsp;&nbsp;序：</dt>
                    <dd>
                        <input type="text" class="ui-validate" isnumber="true" field="OrderIndex" id="OrderIndex" style="width:30px" /><span class="msg">显示的顺序,如:1</span>
                    </dd>
                </dl>
                <dl>
                    <dt>打开方式：</dt>
                    <dd>
                        <span class="input">
                            <select class="tb_normal" field="Target">
                                <option value="">本窗口</option>
                                <option value="_blank">新窗口</option>
                            </select>
                        </span>
                    </dd>
                </dl>

                <dl>
                    <dt>是否显示：</dt>
                    <dd>
                        <input id="visible1" type="radio" name="Visible" field="Visible" value="True" checked="checked" style="border:none" />
                        <label for="visible1">显示</label>
                        <input id="visible2" type="radio" name="Visible" field="Visible" value="False" style="border:none" />
                        <label for="visible2">隐藏</label>
                    </dd>
                </dl>

                <dl><dt>&nbsp;</dt><dd><a id="btn" field="Btn" class="btn" href="javascript:;"></a></dd></dl>




            </div>

            <div id="bind">
                继承于:<br /><select field="Pid" id="pid" class="tb_normal">
                    <option value="0">一不继承一</option>${plinks}</select>
                <br />

                绑定栏目:<br />
                <div>
                    <select name="categoryid" field="CategoryId" class="left tb_normal" style="width:200px">
                        <option value="">一请选择栏目一</option>
                        ${categoryNodes}
                    </select><a class="btn" href="javascript:;" onclick="bind({id:this.previousSibling.value,text:this.previousSibling.options[this.previousSibling.selectedIndex].innerHTML,type:'category'});return false;">绑定</a>

                </div>

                <div class="clearfix"></div>
                绑定文档:<br />

                <input type="text" id="title" class="left" style="width:192px" /><a class="btn" onclick="searchArchive('title');" href="javascript:;">搜索</a>
                <div class="archivelist clearfix">

                </div>

            </div>

        </div>

    </form>
    $js()
    <script type="text/javascript">
        cms.json.bind('dataPanel',${entity});

        var bindid = cms.$('bindid'),
              bindtype = cms.$('bindtype'),
              bindtip = cms.$('bindtip'),
              cancelbind = cms.$('cancelbind'),
              linkurl = cms.$('urlpanel');

        cancelbind.style.display = bindtip.innerHTML == '未绑定' ? 'none' : '';
        //linkurl.style.display = cancelbind.style.display != 'none' ? 'none' : '';

        var upload = cms.upload({
            id: 'upload',
            url: '?module=upload&action=uploadimage&for=link&upload.id=upload',
            exts: '*.gif;*.jpg;*.png;*.bmp'
        },function (result, data) {
            if (result) {
                cms.$('imgurl').value = data.url;
            } else {
                alert('上传失败：' + data);
            }
        });

        function searchArchive(id) {
            var key = cms.val(id);
            var panel = document.getElementsByClassName('archivelist')[0];
            if (key != '') {

                panel.innerHTML = '获取搜索结果...';
                cms.xhr.post('?', 'module=ajax&action=GetSearchArchivesJsonResult&key='
                    + encodeURIComponent(key), function (result) {
                    var data = cms.toJson(result);
                    panel.innerHTML = '';
                    for (var i = 0; i < data.length; i++) {
                        var ele = document.createElement("DIV");
                        ele.innerHTML = data[i].title.replace(key, '<span style="color:red">'
                            + key + '</span>') + '<a href="javascript:bind({id:\'' + data[i].id
                            + '\',text:\'' + data[i].title + '\',type:\'archive\'})">绑定</a>';

                        panel.appendChild(ele);
                    }
                }, function () {
                    panel.innerHTML = '<span>获取搜索结果异常!</span>';
                });
            }
        }

        function bind(data) {
            var id = data.id,
              text = data.text,
              type = data.type;

            if (id != '') {
                var typeStr = '';
                switch (type) {
                    case 'archive': typeStr = '文档'; break;
                    case 'category': typeStr = '栏目'; break;
                }

                bindid.value = id;
                bindtype.value = type;
                bindtip.innerHTML = typeStr + ':' + text;
                cms.$('cancelbind').style.display = '';
                linkurl.style.display = 'none';
            }
        }


        function cancelBind() {
            bindid.value = '';
            bindtype = '';
            bindtip.innerHTML = '未绑定';
            cms.$('cancelbind').style.display = 'none';
            linkurl.style.display = '';
        }

        window.saveData = function onsubmit() {

            if (cms.validator.validate()) {
                cms.xhr.jsonPost(
                       '?module=link&action=save'
                       , cms.json.toObject('dataPanel')
                       , function (json) {
                           if (json.result) {
                               parent.M.alert(json.message, function () {
                                   location.replace('?module=link&action=list&type=${linktype}');
                                   
                               });

                           } else {
                               parent.M.alert(json.message);
                           }

                       });

            }
        };


        cms.$('btn').onclick = window.saveData;

    </script>
</body>
</html>